﻿#include "GameUtil.h"
#include "GameOverLayer.h"
#include "WaveEndedLayer.h"
#include "PauseLayer.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;

GameUtil* GameUtil::d_ins = NULL;

GameUtil::GameUtil(CCScene* scene)
{
	d_setting = new Setting();
	d_ins = this;
	d_scene = scene;
}

GameUtil::~GameUtil(void)
{
	d_setting->~Setting();
	//d_ins = NULL;

}

GameUtil* GameUtil::instance() {
	return d_ins;
}


void GameUtil::gameover() 
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->end();
	GameOverLayer* layer = GameOverLayer::create();
	layer->gameOver(d_scene);
}


void GameUtil::waveEnded() 
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->end();
	WaveEndedLayer* layer = WaveEndedLayer::create();
	layer->waveEnded(d_scene);
}


void GameUtil::pause() {
	PauseLayer* layer = PauseLayer::create();
	layer->paused(d_scene);
}

void GameUtil::updateVolume()
{
	float bgVolume = getSetting()->getBgVolume();
	float effectVolume = getSetting()->getEffectVolume();
	CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(bgVolume/100);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->setEffectsVolume(effectVolume/100);
}

CCRect GameUtil::getRect(float percent, CCSprite* sprite)
{
	CCSize spriteSize = sprite->getContentSize();

	float width = spriteSize.width * percent;
	float height = spriteSize.height * percent;
	CCPoint pos = sprite->getParent()->convertToWorldSpace(sprite->getPosition());
	CCRect rect = CCRectMake(pos.x - width/2,
		pos.y - height/2,
		width,
		height);
	return rect;
}
